package com.wang.service.impl;

import com.wang.entity.Cart;
import com.wang.entity.Product;
import com.wang.mapper.CartMapper;
import com.wang.mapper.ProductMapper;
import com.wang.service.CartService;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import com.wang.vo.CartVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname CartServiceImpl
 * @Description TODO
 * @Date 2024/10/14 8:42
 * @Created by pc
 */
@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private ProductMapper productMapper;
    @Override
    public ResponseUtil findCountByUserId(Integer userId) {
        Integer count = cartMapper.findCountByUserId(userId);
        return ResponseUtil.get(ResponseEnum.CART_FIND_COUNT,count);
    }

    @Override
    public ResponseUtil findCartByUserId(Integer userId) {
        // 根据用户id查询用户购物车详情
        List<Cart> cartList = cartMapper.findCartByUserId(userId);
        List<CartVo> cartVoList = new ArrayList<>();
        cartList.forEach(cart -> {
            // 遍历每一条购物车数据 取出数据进行数据的组装  组装成cartVo
            CartVo cartVo = new CartVo();
            // copyProperties 将第一个参数对象中的属性赋值给第二个参数对象的同名属性
            BeanUtils.copyProperties(cart,cartVo);
            Product product = productMapper.findById(cart.getProductId());
            cartVo.setName(product.getName());
            cartVo.setPrice(product.getPrice());
            cartVoList.add(cartVo);
        });
        return ResponseUtil.get(ResponseEnum.CART_FIND_CART_INFO,cartVoList);
    }

    @Override
    public ResponseUtil updateCart(Integer id, Integer quantity, Integer checked) {
        Integer result = cartMapper.updateCart(id,quantity,checked);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_UPDATE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_UPDATE_CART_FAILURE);
    }

    @Override
    public ResponseUtil delCartById(Integer id) {
        Integer result = cartMapper.delCartById(id);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE);
    }

    @Override
    public ResponseUtil delCartByIds(Integer[] idArr) {
        Integer result = cartMapper.delCartByIds(idArr);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE);
    }

    @Override
    public ResponseUtil delCartByUserId(Integer userId) {
        Integer result = cartMapper.delCartByUserId(userId);
        return result.intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.CART_DELETE_CART_SUCCESS)
                :ResponseUtil.get(ResponseEnum.CART_DELETE_CART_FAILURE);
    }

}
